package com.buyantech.smweb.json;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import javacommon.base.BaseManager;
import javacommon.util.DateTimeUtil;

import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.record.formula.IntPtg;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import cn.jpush.api.examples.JPushClientExample;
import cn.org.rapid_framework.page.Page;
import cn.sudatong.api.Sudatong;

import com.buyantech.smweb.model.Ad;
import com.buyantech.smweb.model.Alliance;
import com.buyantech.smweb.model.AutoPater;
import com.buyantech.smweb.model.Black;
import com.buyantech.smweb.model.CreditsExchange;
import com.buyantech.smweb.model.ExchangeType;
import com.buyantech.smweb.model.Ip;
import com.buyantech.smweb.model.Messages;
import com.buyantech.smweb.model.ObtainIntegral;
import com.buyantech.smweb.model.Setting;
import com.buyantech.smweb.model.Suggestion;
import com.buyantech.smweb.model.User;
import com.buyantech.smweb.service.AdManager;
import com.buyantech.smweb.service.AllianceManager;
import com.buyantech.smweb.service.AutoPaterManager;
import com.buyantech.smweb.service.BlackManager;
import com.buyantech.smweb.service.CreditsExchangeManager;
import com.buyantech.smweb.service.ExchangeTypeManager;
import com.buyantech.smweb.service.IpManager;
import com.buyantech.smweb.service.MessagesManager;
import com.buyantech.smweb.service.ObtainIntegralManager;
import com.buyantech.smweb.service.SettingManager;
import com.buyantech.smweb.service.SuggestionManager;
import com.buyantech.smweb.service.UserManager;
import com.buyantech.smweb.vo.ResultStatusDto;
import com.buyantech.smweb.vo.query.AllianceQuery;
import com.buyantech.smweb.vo.query.CreditsExchangeQuery;
import com.buyantech.smweb.vo.query.ExchangeTypeQuery;
import com.buyantech.smweb.vo.query.ObtainIntegralQuery;
import com.buyantech.smweb.vo.query.UserQuery;
import com.google.gson.Gson;

/**
 * 用户管理
 */
@Controller
public class UserJsonController {

	@Autowired
	private SettingManager settingManager;

	@Autowired
	private UserManager userManager;

	@Autowired
	private AllianceManager allianceManager;

	@Autowired
	private ObtainIntegralManager obtainIntegralManager;

	@Autowired
	private ExchangeTypeManager exchangeTypeManager;

	@Autowired
	private CreditsExchangeManager creditsExchangeManager;

	@Autowired
	private SuggestionManager suggestionManager;

	@Autowired
	private MessagesManager messagesManager;

	@Autowired
	private IpManager ipManager;

	@Autowired
	private AdManager adManager;

	@Autowired
	private BlackManager blackManager;

	@Autowired
	private AutoPaterManager autoPaterManager;

	private static final byte secret = 0x11;

	// 注册用户
	@RequestMapping("/getUserId")
	public ModelAndView getUserId(HttpServletRequest request,
			HttpServletResponse response) {
		String version = request.getParameter("version");
		String IDFA = request.getParameter("IDFA");
		String UDID = request.getParameter("UDID");
		String MAC = request.getParameter("MAC");
		String IMEI = request.getParameter("IMEI");
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		User user = new User();
		String IP = request.getRemoteAddr();
		user.setIP(IP);
		int userid = 0;
		boolean isf = false;
		do {
			userid = (int) (Math.random() * 1000000);
			List<User> list = userManager.getHql("from User where userid='"
					+ userid + "'");
			if (list.size() > 0) {
				isf = true;
			} else {
				isf = false;
			}
		} while (isf);
		String previousDateTime = DateTimeUtil
				.getPreviousDateTimeBySecondString(1200);// 20分钟内
		List list1 = autoPaterManager.getHql(" from AutoPater where IP='"
				+ request.getRemoteAddr() + "' and crdate>='"
				+ previousDateTime + "'");// ip限制
		if (list1 != null && !list1.isEmpty() && list1.get(0) != null) {

			AutoPater autoPater = (AutoPater) list1.get(0);
			String paterId = autoPater.getPaterId();
			List<User> list = userManager.getHql("from User where id='"
					+ paterId + "'");
			if (list != null && list.size() > 0 && list.get(0) != null
					&& user.getIP() != null && list.get(0).getIP() != null) {// user22邀请者，user被邀请者
				User user22 = list.get(0);
				String[] userIP = user.getIP().split("\\.");
				String[] paterIP = user22.getIP().split("\\.");
				if (userIP[0].equals(paterIP[0])
						&& userIP[1].equals(paterIP[1])) {
				} else {
					user.setPaterid(user22.getId());
					user.setPaterDate(DateTimeUtil.getDateTimeString());
					user22.setExtensionNumber(user22.getExtensionNumber() + 1);
					userManager.update(user22);
				}
				autoPaterManager.removeById(autoPater.getId());
			}
		}
		user.setUserid(userid + "");
		user.setRegister(DateTimeUtil.getDateTimeString());
		user.setState(1);
		user.setUsername("赚赚" + userid);
		if (version != null)
			user.setVersion(version);
		if (IDFA != null)
			user.setIDFA(IDFA);
		if (UDID != null)
			user.setUDID(UDID);
		if (MAC != null)
			user.setMAC(MAC);
		if (IMEI != null)
			user.setIMEI(IMEI);
		userManager.save(user);
		ResultStatusDto result = new ResultStatusDto("100", "保存成功");
		modelMap.put("status", result);
		modelMap.put("id", user.getId());
		modelMap.put("userid", userid);
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 2.获取总积分
	@RequestMapping("/getTotalNum")
	public ModelAndView getTotalNum(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("userId") String userId) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			User user = userManager.getById(userId);
			List<ObtainIntegral> list1 = obtainIntegralManager
					.getHql(" from ObtainIntegral where userid='"
							+ user.getUserid()
							+ "' and type=1 and childid is null order by crdate desc");
			if (list1 != null && list1.size() > 0 && list1.get(0) != null) {
				ObtainIntegral obtainIntegral = (ObtainIntegral) list1.get(0);
				obtainIntegral.setIP(request.getRemoteAddr());
				obtainIntegralManager.update(obtainIntegral);
			}
			ResultStatusDto result = new ResultStatusDto("100", "保存成功");
			modelMap.put("status", result);
			modelMap.put("totalNum", user.getIntegral() - user.getHuIntegral());
		} catch (DataAccessException e) {
			ResultStatusDto result = new ResultStatusDto("104", "服务器出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 7. 兑换话费
	@RequestMapping("/submitPay")
	public ModelAndView submitPay(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("number") String number,
			@RequestParam("itemid") String itemid) {
		String version = request.getParameter("version");
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("exchange");
		double interval = settingManager.exchange;
		//
		// Setting setting14 = settingManager.getType("QBnumD");
		// Setting setting15 = settingManager.getType("QBnumM");
		// Setting setting16 = settingManager.getType("HFnumD");
		// Setting setting17 = settingManager.getType("HFnumM");
		int QBnumD = settingManager.QBnumD;
		int QBnumM = settingManager.QBnumM;
		int HFnumD = settingManager.HFnumD;
		;
		int HFnumM = settingManager.HFnumM;

		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String startTimeOfDay = dateFm.format(new java.util.Date());
		dateFm = new SimpleDateFormat("yyyy-MM-01 00:00:00");
		String startTimeOfMonth = dateFm.format(new java.util.Date());

		try {

			ExchangeType exchangeType = exchangeTypeManager.getById(itemid);

			// 限制每日兑换数量start
			if (exchangeType.getTypename().equals("Q币")) {
				if (QBnumD != 0) {
					List list1 = creditsExchangeManager
							.getHql("select sum(money) from CreditsExchange where typeStr='Q币' and  account='"
									+ number
									+ "' and crdate>='"
									+ startTimeOfDay + "'");
					if (list1 != null && list1.size() > 0
							&& list1.get(0) != null) {
						if ((Long) list1.get(0) + exchangeType.getMoney() > QBnumD) {
							ResultStatusDto result = new ResultStatusDto("105",
									"今日QB兑换已超过限制");
							modelMap.put("status", result);
							modelView.addAllObjects(modelMap);
							return modelView;
						}
					}
				}

				if (QBnumM != 0) {
					List list2 = creditsExchangeManager
							.getHql("select sum(money) from CreditsExchange where typeStr='Q币' and  account='"
									+ number
									+ "' and crdate>='"
									+ startTimeOfMonth + "'");
					if (list2 != null && list2.size() > 0
							&& list2.get(0) != null) {
						if ((Long) list2.get(0) + exchangeType.getMoney() > QBnumM) {
							ResultStatusDto result = new ResultStatusDto("105",
									"本月QB兑换已超过限制");
							modelMap.put("status", result);
							modelView.addAllObjects(modelMap);
							return modelView;
						}

					}
				}
			}

			if (exchangeType.getTypename().equals("话费")) {
				if (HFnumD != 0) {
					List list3 = creditsExchangeManager
							.getHql("select sum(money) from CreditsExchange where typeStr='话费' and  account='"
									+ number
									+ "' and crdate>='"
									+ startTimeOfDay + "'");
					if (list3 != null && list3.size() > 0
							&& list3.get(0) != null) {
						if ((Long) list3.get(0) + exchangeType.getMoney() > HFnumD) {
							ResultStatusDto result = new ResultStatusDto("105",
									"今日话费兑换已超过限制");
							modelMap.put("status", result);
							modelView.addAllObjects(modelMap);
							return modelView;
						}
					}
				}
				if (HFnumM != 0) {
					List list4 = creditsExchangeManager
							.getHql("select sum(money) from CreditsExchange where typeStr='话费' and  account='"
									+ number
									+ "' and crdate>='"
									+ startTimeOfMonth + "'");
					if (list4 != null && list4.size() > 0
							&& list4.get(0) != null) {
						if ((Long) list4.get(0) + exchangeType.getMoney() > HFnumM) {
							ResultStatusDto result = new ResultStatusDto("105",
									"本月话费兑换已超过限制");
							modelMap.put("status", result);
							modelView.addAllObjects(modelMap);
							return modelView;
						}
					}
				}
			}
			// 限制每日兑换数量end

			User user = userManager.getById(userId);
			Date rDate = DateTimeUtil.toDate(user.getRegister());
			Date nowDate = new Date();
			double differ = DateTimeUtil.compareHour(nowDate, rDate);
			if (differ > interval) {
				int yuIntegral = user.getIntegral() - user.getHuIntegral();
				if (yuIntegral >= exchangeType.getIntegral()) {
					CreditsExchange crachange = new CreditsExchange();
					int isBlack = 0;

					boolean isErr = false;
					try {
						// 规则A
						List<Black> list1 = blackManager
								.getHql(" from Black where account='" + number
										+ "'");
						if (list1 != null && !list1.isEmpty()
								&& list1.get(0) != null) {
							isErr = true;
							crachange.setIsCheck(1);
							crachange.setReason("未通过规则A");
							isBlack = 1;
						}
						//
						// 规则B
						if (!isErr) {
							List list2 = creditsExchangeManager
									.getHql("select distinct account from CreditsExchange where userid='"
											+ user.getUserid() + "'");
							if (list2 != null && !list2.isEmpty()
									&& list2.get(0) != null
									&& !list2.get(0).equals(number)) {
								for (Object account : list2) {
									List<Black> list3 = blackManager
											.getHql(" from Black where account='"
													+ (String) account + "'");
									if (list3 != null && !list3.isEmpty()
											&& list3.get(0) != null) {
										isErr = true;
										crachange.setIsCheck(1);
										crachange.setReason("未通过规则B");
										break;
									}
								}
							}
						}
						//

						/*
						 * // 规则C if (!isErr) { List list2 =
						 * creditsExchangeManager
						 * .getHql(" from CreditsExchange where userid='" +
						 * user.getUserid() + "' " + "and account='" + number +
						 * "' and crdate='" + DateTimeUtil.getDateTimeString() +
						 * "' and money=" + exchangeType.getMoney()); if (list2
						 * != null && !list2.isEmpty() && list2.get(0) != null)
						 * { isErr = true; crachange.setIsCheck(1);
						 * crachange.setReason("未通过规则C"); } } //
						 */

						// 规则D
						if (!isErr) {
							// Setting setting18 = settingManager
							// .getType("numMax");
							int numMax = settingManager.numMax;
							if (numMax != 0) {
								List list2 = obtainIntegralManager
										.getHql("SELECT count(*) FROM ObtainIntegral WHERE userid='"
												+ user.getUserid()
												+ "' AND appName is NOT NULL AND type<>5 AND number<>0 AND crdate>'"
												+ DateTimeUtil
														.getPreviousDateTimeString(1)
												+ "' GROUP BY appName,number");
								if (list2 != null && !list2.isEmpty()
										&& list2.get(0) != null) {
									for (Object num : list2) {
										if ((Long) num > numMax) {// 可配置
											isErr = true;
											crachange.setIsCheck(1);
											crachange.setReason("未通过规则D");
											break;
										}
									}
								}
								if (!isErr) {
									List list3 = obtainIntegralManager
											.getHql("SELECT count(*) FROM ObtainIntegral WHERE userid='"
													+ user.getUserid()
													+ "' AND appName is NULL AND type<>5 AND number<>0 AND crdate>'"
													+ DateTimeUtil
															.getPreviousDateTimeString(1)
													+ "' GROUP BY remark,number");
									if (list3 != null && !list3.isEmpty()
											&& list3.get(0) != null) {
										for (Object num : list3) {
											if ((Long) num > numMax) {// 可配置
												isErr = true;
												crachange.setIsCheck(1);
												crachange.setReason("未通过规则D");
												break;
											}
										}
									}
								}
							}
						}

						if (!isErr) {// 规则E
							List<User> list = userManager
									.getHql("from User where id='"
											+ user.getPaterid() + "'");
							if (list != null && !list.isEmpty()
									&& list.get(0) != null) {
								User pater = list.get(0);
								// if (pater.getIP() != null
								// && user.getIP() != null) {
								// String[] userIP = user.getIP().split("\\.");
								// String[] paterIP = pater.getIP().split(
								// "\\.");
								// if (userIP[0].equals(paterIP[0])
								// && userIP[1].equals(paterIP[1])) {
								// isErr = true;
								// crachange.setIsCheck(1);
								// crachange.setReason("未通过规则E");
								// }
								// }

								// 规则F
								if (!isErr && pater.getExtensionNumber() >= 4) {
									List list2 = creditsExchangeManager
											.getHql("select distinct account from CreditsExchange where userid='"
													+ user.getUserid() + "'");
									if (list2 == null)
										list2 = new ArrayList();
									if (!list2.contains(number)) {
										list2.add(number);
									}
									for (Object account : list2) {
										List list3 = creditsExchangeManager
												.getHql("from CreditsExchange where  account='"
														+ (String) account
														+ "' and userid='"
														+ list.get(0)
																.getUserid()
														+ "'");
										if (list3 != null && !list3.isEmpty()
												&& list3.get(0) != null) {
											isErr = true;
											crachange.setIsCheck(1);
											crachange.setReason("未通过规则F");
											break;
										}
									}
								}
							}
							//

						}

						// // 规则G
						// if (!isErr) {
						// List<User> list = userManager
						// .getHql("from User where paterid='"
						// + user.getId()
						// + "' ORDER BY register DESC LIMIT 0,5");
						// if (list != null && list.size() > 0
						// && list.get(0) != null
						// && user.getIP() != null) {
						// String[] userIP = user.getIP().split("\\.");
						//
						// for (User child : list) {
						// if (child.getIP() != null) {
						// String[] childIP = child.getIP().split(
						// "\\.");
						// if (userIP[0].equals(childIP[0])
						// && userIP[1].equals(childIP[1])) {
						// isErr = true;
						// crachange.setIsCheck(1);
						// crachange.setReason("未通过规则G");
						// break;
						// }
						// }
						// }
						// }
						// }

						// 规则H
						if (!isErr) {
							List list2 = creditsExchangeManager
									.getHql("select distinct account from CreditsExchange where userid='"
											+ user.getUserid() + "'");
							StringBuffer accounts = new StringBuffer();
							if (list2 == null)
								list2 = new ArrayList();
							if (!list2.contains(number)) {
								list2.add(number);
							}
							accounts.append("(");
							for (Object account : list2) {
								accounts.append("'");
								accounts.append((String) account);
								accounts.append("',");

							}
							accounts.deleteCharAt(accounts.length() - 1);
							accounts.append(")");
							List<User> list = userManager
									.getHql("from User where paterid='"
											+ user.getId()
											+ "' ORDER BY register DESC LIMIT 0,10");
							if (list != null && list.size() > 0
									&& list.get(0) != null) {
								for (User child : list) {
									List list4 = creditsExchangeManager
											.getHql("select distinct account from CreditsExchange where userid='"
													+ child.getUserid() + "'");
									if (list4 != null && list4.size() >= 4) {
										List list3 = creditsExchangeManager
												.getHql("from CreditsExchange where  account in"
														+ accounts.toString()
														+ " and userid='"
														+ child.getUserid()
														+ "'");
										if (list3 != null && !list3.isEmpty()
												&& list3.get(0) != null) {
											{
												isErr = true;
												crachange.setIsCheck(1);
												crachange.setReason("未通过规则H");
												break;
											}
										}
									}

								}
							}
						}
					} catch (Exception e) {

					}

					// List<CreditsExchange> list = creditsExchangeManager
					// .getHql(" from CreditsExchange where account='"
					// + number + "'");
					// int isBlack = 0;
					// if (list != null && !list.isEmpty() && list.get(0) !=
					// null) {
					// for (CreditsExchange mCreditsExchange : list) {
					// isBlack = mCreditsExchange.getIsBlack();
					// break;
					// }
					// }
					crachange.setAccount(number);
					crachange.setIsBlack(isBlack);
					crachange.setUserid(user.getUserid());
					crachange.setUsername(user.getUsername());
					crachange.setIntegral(exchangeType.getIntegral());
					crachange.setMoney(exchangeType.getMoney());
					crachange.setTypeStr(exchangeType.getTypename());
					crachange.setState("1");
					String IP = request.getRemoteAddr();
					crachange.setIP(IP);
					if (version != null)
						crachange.setVersion(version);
					crachange.setCrdate(DateTimeUtil.getDateTimeString());
					User user11 = userManager.getById(userId);
					int yuIntegral1 = user11.getIntegral()
							- user11.getHuIntegral();

					boolean isNeedCheck = false;
					String orderId = null;
					if (yuIntegral1 >= exchangeType.getIntegral()) {
						if (crachange.getIsCheck() == 0) {
							String recharge = SettingManager.recharge;
							if (recharge != null && recharge.equals("true")) {
								if (crachange.getTypeStr().equals("Q币")) {
									orderId = Sudatong.getOrderId();
									int status = Sudatong.rechargeQbi(
											crachange.getMoney(), number,
											orderId);
									if (status == 1 || status == -1
											|| status == 9) {
										crachange.setState("2");
										crachange.setRemark("自动充值成功");
									} else if (status == 0 || status == 5) {
										crachange.setRemark("平台正在处理中");
										isNeedCheck = true;
									}else{
										crachange.setRemark("自动充值失败");
									}

								} else if (crachange.getTypeStr().equals("话费")) {
									orderId = Sudatong.getOrderId();
									int status = Sudatong.rechargeHuafei(
											crachange.getMoney(), number,
											orderId);
									if (status == 1 || status == -1
											|| status == 9) {
										crachange.setState("2");
										crachange.setRemark("自动充值成功");
									} else if (status == 0 || status == 5) {
										crachange.setRemark("平台正在处理中");
										isNeedCheck = true;
									}else{
										crachange.setRemark("自动充值失败");
									}
								}
							}

						}
						creditsExchangeManager.save(crachange);
						if (isNeedCheck) {
							Timer timer = new Timer();
							TimerTask timerTask = new statusTask(orderId,
									crachange, timer);
							timer.schedule(timerTask, 1000*60*5, 1000*60*5);
						}
						user11.setHuIntegral(user11.getHuIntegral()
								+ exchangeType.getIntegral());
						userManager.update(user11);
						ResultStatusDto result = new ResultStatusDto("100",
								"兑换成功，请您耐心等待，我们将在24小时内为您支付,节假日顺延");
						modelMap.put("status", result);
					} else {
						ResultStatusDto result = new ResultStatusDto("102",
								"积分不足，再去赚点吧*^_^*");
						modelMap.put("status", result);
					}
				} else {
					ResultStatusDto result = new ResultStatusDto("102",
							"积分不足，再去赚点吧*^_^*");
					modelMap.put("status", result);
				}
			} else {
				ResultStatusDto result = new ResultStatusDto("103", "必须大于"
						+ interval + "小时才能兑换");
				modelMap.put("status", result);
			}
		} catch (DataAccessException e) {
			ResultStatusDto result = new ResultStatusDto("104", "服务器出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 20. 获取可利用积分平台，
	@RequestMapping("/getJifenPlatform")
	public ModelAndView getJifenPlatform(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("clientType") String clientType) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			AllianceQuery query = new AllianceQuery();
			query.setState("0");
			query.setPageSize(500);
			query.setClientType(clientType);
			Page page = allianceManager.searchPage(query);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
			modelMap.put("result", page.getResult());
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 提交积分
	@RequestMapping("/submitNum1")
	// 新的
	public ModelAndView submitNum1(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("addNum") int addNum,
			@RequestParam("type") String type, @RequestParam("isErr") int isErr) {// 0表示
																					// 正常，1表示异常
		String version = request.getParameter("version");

		if (version == null)
			return null;
		float versionNum = Float.parseFloat(version);
		if (versionNum < 1.7)
			return null;

		// 限制同一IP的最大用户数（后台可配置）start
		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String time = dateFm.format(new java.util.Date());
		String previousDateTime = DateTimeUtil.getPreviousDateTimeString(3);// 72小时内
		//
		// List<ObtainIntegral>
		// list=obtainIntegralManager.getHql("from ObtainIntegral where IP='"+request.getRemoteAddr()+"' and crdate>='"+time+"'"
		// );
		// Setting setting2 = settingManager.getType("usernum");
		int usernum = settingManager.usernum;
		// Setting setting3 = settingManager.getType("usernum1");
		int usernum1 = settingManager.usernum1;
		// Setting setting4 = settingManager.getType("usernum2");
		int usernum2 = settingManager.usernum2;
		// Setting setting5 = settingManager.getType("num");
		int num1 = settingManager.num;

		// if(list.size()>=usernum)
		// isErr=1;
		// 限制同一IP的最大用户数（后台可配置）end

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			String IP = request.getRemoteAddr();
			User user = userManager.getById(userId);

			// 黑名单ip start
			List ips = ipManager.getHql("select DISTINCT ip from Ip");
			if (ips != null && ips.size() > 0 && ips.get(0) != null) {
				for (Object ip : ips) {
					if (!ip.equals("") && IP.startsWith((String) ip)) {
						isErr = 1;
						break;
					}
				}
			}
			// 黑名单ip end

			if (usernum > 0 && isErr != 1) {
				List list1 = obtainIntegralManager
						.getHql("select DISTINCT userid from ObtainIntegral where IP='"
								+ request.getRemoteAddr()
								+ "' and crdate>='"
								+ previousDateTime + "' and fnumber<>0");// ip限制
				if (list1 != null && list1.size() >= usernum
						&& list1.get(0) != null) {
					if (!list1.contains(user.getUserid()))
						isErr = 1;
				}
			}
			String str = IP.substring(0, IP.lastIndexOf('.'));
			String str1 = str.substring(0, str.lastIndexOf('.'));
			if (usernum1 > 0 && isErr != 1) {
				List list1 = obtainIntegralManager
						.getHql("select DISTINCT userid from ObtainIntegral where IP like '"
								+ str
								+ "%' and crdate>='"
								+ time
								+ "' and fnumber<>0");// ip限制
				if (list1 != null && list1.size() >= usernum1
						&& list1.get(0) != null) {
					if (!list1.contains(user.getUserid()))
						isErr = 1;
				}
			}

			if (usernum2 > 0 && isErr != 1) {
				List list1 = obtainIntegralManager
						.getHql("select DISTINCT userid from ObtainIntegral where IP like '"
								+ str1
								+ "%' and crdate>='"
								+ time
								+ "' and fnumber<>0");// ip限制
				if (list1 != null && list1.size() >= usernum2
						&& list1.get(0) != null) {
					if (!list1.contains(user.getUserid()))
						isErr = 1;
				}
			}

			Alliance alliance = allianceManager.getById(type);

			if (user != null) {
				int myNum = addNum;
				// Setting setting1 = settingManager.getType("integral");
				int integral = settingManager.integral;
				int temp = myNum;
				if (myNum > integral) {// 如果用户单次从某渠道获取积分大于600分（数据可配置），则系统随机给用给用户随机返回200-400积分
					Random rd = new Random();
					myNum = rd.nextInt(201) + 200;
				}

				if (num1 > 0 && isErr != 1) {
					List list1 = obtainIntegralManager
							.getHql("select count(*) from ObtainIntegral where userid='"
									+ user.getUserid()
									+ "' and remark='"
									+ alliance.getName()
									+ "' and crdate>='"
									+ time + "' and fnumber>0 group by fnumber");// 同一个区域获取的相同积分个数最大配置
					if (list1 != null && list1.size() > 0
							&& list1.get(0) != null) {
						for (Object num : list1) {
							if ((Long) num >= num1) {
								isErr = 1;
								break;
							}
						}
					}
				}

				if (isErr != 1 && user.getPaterid() != null
						&& !user.getPaterid().equals("")) {
					User userP = userManager.getById(user.getPaterid());
					// Setting setting = settingManager.getType("ratio");
					int ratio = settingManager.ratio;
					int sss = (myNum * ratio) / 100;
					int num = (integral * ratio) / 100 - 1;
					if (sss > 0 && sss <= num) {// 不超过 才返回
						// myNum=addNum-sss;
						ObtainIntegral obint = new ObtainIntegral();
						obint.setUserid(userP.getUserid());
						obint.setUsername(userP.getUsername());
						obint.setNumber(sss);
						obint.setFnumber(sss);
						obint.setType(1);
						obint.setRemark(alliance.getName());
						obint.setChildid(user.getId());// 奖励添加下线id
						obint.setCrdate(DateTimeUtil.getDateTimeString());
						obtainIntegralManager.save(obint);
						userP.setIntegral(userP.getIntegral() + sss);
						userP.setDintegral(userP.getDintegral() + sss);
						userManager.update(userP);
					}
				}
				ObtainIntegral obint = new ObtainIntegral();
				obint.setUserid(user.getUserid());
				obint.setUsername(user.getUsername());
				obint.setNumber(temp);
				if (isErr == 0) {
					obint.setFnumber(myNum);
				}
				obint.setIP(IP);
				obint.setType(1);
				obint.setRemark(alliance.getName());
				if (version != null)
					obint.setVersion(version);
				obint.setCrdate(DateTimeUtil.getDateTimeString());
				obtainIntegralManager.save(obint);
				// synchronized (this.getClass()) {
				// user = userManager.getById(userId);
				if (isErr == 0) {
					// user.setIntegral(user.getIntegral() + myNum);
					userManager.update("UPDATE User SET integral=integral+"
							+ myNum + ", dintegral=dintegral+" + temp
							+ " WHERE id='" + userId + "'");
				} else {
					userManager.update("UPDATE User SET  dintegral=dintegral+"
							+ temp + " WHERE id='" + userId + "'");
				}
				// user.setDintegral(user.getDintegral() + temp);
				// }
				ResultStatusDto result = new ResultStatusDto("100", "保存成功");
				modelMap.put("status", result);
			} else {
				ResultStatusDto result = new ResultStatusDto("144", "没有此用户");
				modelMap.put("status", result);
			}
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "保存出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 提交积分
	@RequestMapping("/submitNum11")
	// 新的
	public ModelAndView submitNum11(HttpServletRequest request,
			HttpServletResponse response) throws JSONException {// 0表示
		// 正常，1表示异常
		String version = null;
		String userId = null;
		String type = null;
		int addNum;
		int isErr;
		System.out.println("全部字符串:" + request.getQueryString());
		try {
			version = request.getParameter("version");
			userId = request.getParameter("userId");
			type = request.getParameter("type");
			// System.out.println("userId：" + request.getParameter("userId"));
			// System.out.println("addNum：" + request.getParameter("addNum"));
			// System.out.println("addNum："
			// + deCode(request.getParameter("addNum")));
			addNum = Integer.parseInt(deCode(request.getParameter("addNum")));

			isErr = Integer.parseInt(request.getParameter("isErr"));
		} catch (Exception e) {
			return null;
		}

		// JSONObject map = getData(request);
		// if (map == null)
		// return null;
		// String userId = (String) map.get("userId");
		// String type = (String) map.get("type");
		if (userId == null || type == null)
			return null;
		// int addNum = (Integer) map.get("addNum");
		// int isErr = (Integer) map.get("isErr");
		// String version = (String) map.get("version");

		if (version == null)
			return null;
		float versionNum = Float.parseFloat(version);
		if (versionNum < 1.7)
			return null;

		// 限制同一IP的最大用户数（后台可配置）start
		SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
		String time = dateFm.format(new java.util.Date());
		String previousDateTime = DateTimeUtil.getPreviousDateTimeString(3);// 72小时内
		//
		// List<ObtainIntegral>
		// list=obtainIntegralManager.getHql("from ObtainIntegral where IP='"+request.getRemoteAddr()+"' and crdate>='"+time+"'"
		// );
		// Setting setting2 = settingManager.getType("usernum");
		int usernum = settingManager.usernum;
		// Setting setting3 = settingManager.getType("usernum1");
		int usernum1 = settingManager.usernum1;
		// Setting setting4 = settingManager.getType("usernum2");
		int usernum2 = settingManager.usernum2;
		// Setting setting5 = settingManager.getType("num");
		int num1 = settingManager.num;

		// if(list.size()>=usernum)
		// isErr=1;
		// 限制同一IP的最大用户数（后台可配置）end

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			String IP = request.getRemoteAddr();
			User user = userManager.getById(userId);

			// 黑名单ip start
			List ips = ipManager.getHql("select DISTINCT ip from Ip");
			if (ips != null && ips.size() > 0 && ips.get(0) != null) {
				for (Object ip : ips) {
					if (!ip.equals("") && IP.startsWith((String) ip)) {
						isErr = 1;
						break;
					}
				}
			}
			// 黑名单ip end

			if (usernum > 0 && isErr != 1) {
				List list1 = obtainIntegralManager
						.getHql("select DISTINCT userid from ObtainIntegral where IP='"
								+ request.getRemoteAddr()
								+ "' and crdate>='"
								+ previousDateTime + "' and fnumber<>0");// ip限制
				if (list1 != null && list1.size() >= usernum
						&& list1.get(0) != null) {
					if (!list1.contains(user.getUserid()))
						isErr = 1;
				}
			}
			String str = IP.substring(0, IP.lastIndexOf('.'));
			String str1 = str.substring(0, str.lastIndexOf('.'));
			if (usernum1 > 0 && isErr != 1) {
				List list1 = obtainIntegralManager
						.getHql("select DISTINCT userid from ObtainIntegral where IP like '"
								+ str
								+ "%' and crdate>='"
								+ time
								+ "' and fnumber<>0");// ip限制
				if (list1 != null && list1.size() >= usernum1
						&& list1.get(0) != null) {
					if (!list1.contains(user.getUserid()))
						isErr = 1;
				}
			}

			if (usernum2 > 0 && isErr != 1) {
				List list1 = obtainIntegralManager
						.getHql("select DISTINCT userid from ObtainIntegral where IP like '"
								+ str1
								+ "%' and crdate>='"
								+ time
								+ "' and fnumber<>0");// ip限制
				if (list1 != null && list1.size() >= usernum2
						&& list1.get(0) != null) {
					if (!list1.contains(user.getUserid()))
						isErr = 1;
				}
			}

			Alliance alliance = allianceManager.getById(type);

			if (user != null) {
				int myNum = addNum;
				// Setting setting1 = settingManager.getType("integral");
				int integral = settingManager.integral;
				int temp = myNum;
				if (myNum > integral) {// 如果用户单次从某渠道获取积分大于600分（数据可配置），则系统随机给用给用户随机返回200-400积分
					Random rd = new Random();
					myNum = rd.nextInt(201) + 200;
				}

				if (num1 > 0 && isErr != 1) {
					List list1 = obtainIntegralManager
							.getHql("select count(*) from ObtainIntegral where userid='"
									+ user.getUserid()
									+ "' and remark='"
									+ alliance.getName()
									+ "' and crdate>='"
									+ time + "' and fnumber>0 group by fnumber");// 同一个区域获取的相同积分个数最大配置
					if (list1 != null && list1.size() > 0
							&& list1.get(0) != null) {
						for (Object num : list1) {
							if ((Long) num >= num1) {
								isErr = 1;
								break;
							}
						}
					}
				}

				if (isErr != 1 && user.getPaterid() != null
						&& !user.getPaterid().equals("")) {
					User userP = userManager.getById(user.getPaterid());
					// Setting setting = settingManager.getType("ratio");
					int ratio = settingManager.ratio;
					int sss = (myNum * ratio) / 100;
					int num = (integral * ratio) / 100 - 1;
					if (sss > 0 && sss <= num) {// 不超过 才返回
						// myNum=addNum-sss;
						ObtainIntegral obint = new ObtainIntegral();
						obint.setUserid(userP.getUserid());
						obint.setUsername(userP.getUsername());
						obint.setNumber(sss);
						obint.setFnumber(sss);
						obint.setType(1);
						obint.setRemark(alliance.getName());
						obint.setChildid(user.getId());// 奖励添加下线id
						obint.setCrdate(DateTimeUtil.getDateTimeString());
						obtainIntegralManager.save(obint);
						userP.setIntegral(userP.getIntegral() + sss);
						userP.setDintegral(userP.getDintegral() + sss);
						userManager.update(userP);
					}
				}
				ObtainIntegral obint = new ObtainIntegral();
				obint.setUserid(user.getUserid());
				obint.setUsername(user.getUsername());
				obint.setNumber(temp);
				if (isErr == 0) {
					obint.setFnumber(myNum);
				}
				obint.setIP(IP);
				obint.setType(1);
				obint.setRemark(alliance.getName());
				if (version != null)
					obint.setVersion(version);
				obint.setCrdate(DateTimeUtil.getDateTimeString());
				obtainIntegralManager.save(obint);
				// synchronized (this.getClass()) {
				// user = userManager.getById(userId);
				if (isErr == 0) {
					// user.setIntegral(user.getIntegral() + myNum);
					userManager.update("UPDATE User SET integral=integral+"
							+ myNum + ", dintegral=dintegral+" + temp
							+ " WHERE id='" + userId + "'");
				} else {
					userManager.update("UPDATE User SET  dintegral=dintegral+"
							+ temp + " WHERE id='" + userId + "'");
				}
				// user.setDintegral(user.getDintegral() + temp);
				// }
				ResultStatusDto result = new ResultStatusDto("100", "保存成功");
				modelMap.put("status", result);
			} else {
				ResultStatusDto result = new ResultStatusDto("144", "没有此用户");
				modelMap.put("status", result);
			}
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "保存出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 增加积分
	@RequestMapping("/addNum")
	public ModelAndView addNum(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("addNum") int addNum
	// , @RequestParam("type") String type
	) {

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			User user = userManager.getById(userId);
			// Alliance alliance= allianceManager.getById(type);
			if (user != null) {
				int myNum = addNum;
				int temp = myNum;
				ObtainIntegral obint = new ObtainIntegral();
				obint.setUserid(user.getUserid());
				obint.setUsername(user.getUsername());
				obint.setNumber(temp);
				obint.setFnumber(myNum);
				obint.setType(5);
				// obint.setRemark(alliance.getName());
				obint.setCrdate(DateTimeUtil.getDateTimeString());
				obtainIntegralManager.save(obint);
				userManager.update("UPDATE User SET integral=integral+" + myNum
						+ ", dintegral=dintegral+" + temp + " WHERE id='"
						+ userId + "'");
				ResultStatusDto result = new ResultStatusDto("100", "保存成功");
				modelMap.put("status", result);
			} else {
				ResultStatusDto result = new ResultStatusDto("144", "没有此用户");
				modelMap.put("status", result);
			}
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "保存出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 增加积分
	@RequestMapping("/addNum1")
	public ModelAndView addNum1(HttpServletRequest request,
			HttpServletResponse response) throws JSONException {
		String userId = null;
		int addNum;

		try {
			userId = request.getParameter("userId");
			addNum = Integer.parseInt(deCode(request.getParameter("addNum")));
		} catch (Exception e) {
			return null;
		}
		if (userId == null || addNum <= 0)
			return null;
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			User user = userManager.getById(userId);
			// Alliance alliance= allianceManager.getById(type);
			if (user != null) {
				int myNum = addNum;
				int temp = myNum;
				ObtainIntegral obint = new ObtainIntegral();
				obint.setUserid(user.getUserid());
				obint.setUsername(user.getUsername());
				obint.setNumber(temp);
				obint.setFnumber(myNum);
				obint.setType(5);
				// obint.setRemark(alliance.getName());
				obint.setCrdate(DateTimeUtil.getDateTimeString());
				obtainIntegralManager.save(obint);
				userManager.update("UPDATE User SET integral=integral+" + myNum
						+ ", dintegral=dintegral+" + temp + " WHERE id='"
						+ userId + "'");
				ResultStatusDto result = new ResultStatusDto("100", "保存成功");
				modelMap.put("status", result);
			} else {
				ResultStatusDto result = new ResultStatusDto("144", "没有此用户");
				modelMap.put("status", result);
			}
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "保存出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 兑换话费 获取可兑换列表
	@RequestMapping("/getPhonePaylist")
	public ModelAndView getPhonePaylist(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			ExchangeTypeQuery query = new ExchangeTypeQuery();
			query.setPageSize(500);
			query.setTypename("话费");
			Page page = exchangeTypeManager.searchPage(query);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
			modelMap.put("result", page.getResult());
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 6. 获取QQ产品列表
	@RequestMapping("/getQQlist")
	public ModelAndView getQQlist(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			ExchangeTypeQuery query = new ExchangeTypeQuery();
			query.setPageSize(500);
			query.setTypename("Q币");
			Page page = exchangeTypeManager.searchPage(query);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
			modelMap.put("result", page.getResult());
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 8. 获取支付宝列表
	@RequestMapping("/getaliPaylist")
	public ModelAndView getaliPaylist(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			ExchangeTypeQuery query = new ExchangeTypeQuery();
			query.setPageSize(500);
			query.setTypename("支付宝");
			Page page = exchangeTypeManager.searchPage(query);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
			modelMap.put("result", page.getResult());
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 11. 获取财付通列表
	@RequestMapping("/getCaiFuPaylist")
	public ModelAndView getCaiFuPaylist(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			ExchangeTypeQuery query = new ExchangeTypeQuery();
			query.setPageSize(500);
			query.setTypename("财付通");
			Page page = exchangeTypeManager.searchPage(query);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
			modelMap.put("result", page.getResult());
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 12. 积分历史
	@RequestMapping("/getNumHistory")
	public ModelAndView getNumHistory(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("userId") String userId) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		String page1 = request.getParameter("page");
		int page = 1;
		if (page1 != null)
			page = Integer.parseInt(page1);
		try {
			User user = userManager.getById(userId);
			ObtainIntegralQuery query = new ObtainIntegralQuery();
			query.setUserid(user.getUserid());
			query.setPageSize(20);
			query.setSortColumns(" crdate  desc");
			query.setPageNumber(page);
			List<ObtainIntegral> list = obtainIntegralManager
					.searchPage1(query).getResult();
			// CreditsExchangeQuery query1 = new CreditsExchangeQuery();
			// query1.setUserid(user.getUserid());
			// query1.setPageSize(20);
			// query1.setPageNumber(page);
			// query1.setSortColumns(" crdate  desc");
			// List<CreditsExchange> list22 =
			// creditsExchangeManager.findPage(query1)
			// .getResult();
			// List<ObtainIntegral> list = obtainIntegralManager
			// .getHql("from ObtainIntegral where userid='"
			// + user.getUserid() + "' and fnumber<>0 LIMIT 0,20");
			// List<CreditsExchange> list22 = creditsExchangeManager
			// .getHql("from CreditsExchange where userid='"
			// + user.getUserid() + "'");

			modelMap.put("result", list);
			// modelMap.put("result22", list22);

			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 12. 我的推广
	@RequestMapping("/spread")
	public ModelAndView spread(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("userId") String userId) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		String page1 = request.getParameter("page");
		int page = 1;
		if (page1 != null)
			page = Integer.parseInt(page1);
		try {
			UserQuery query = new UserQuery();
			query.setPaterid(userId);
			query.setSortColumns(" paterDate  desc");
			User userP = userManager.getById(userId);
			query.setPageSize(20);
			query.setPageNumber(page);
			List<User> list = userManager.findPage(query).getResult();

			List<ObtainIntegral> list1 = obtainIntegralManager
					.getHql("from ObtainIntegral where userid='"
							+ userP.getUserid()
							+ "' and childid is not null and fnumber<>0");
			Map<String, Integer> map = new HashMap<String, Integer>();
			if (list1 != null && list1.size() > 0 && list1.get(0) != null) {
				for (ObtainIntegral obtainIntegral : list1) {
					if (map.containsKey(obtainIntegral.getChildid())) {
						int sum = map.get(obtainIntegral.getChildid());
						sum += obtainIntegral.getFnumber();
						map.put(obtainIntegral.getChildid(), sum);
					} else {
						map.put(obtainIntegral.getChildid(),
								obtainIntegral.getFnumber());
					}
				}
				for (User user : list) {
					if (map.containsKey(user.getId())) {
						user.setPaterRatio(map.get(user.getId()).toString());
					} else {
						user.setPaterRatio("");
					}
				}
			}
			// for (User user : list) {
			// List list1 = obtainIntegralManager
			// .getHql("select sum(fnumber) from ObtainIntegral where userid='"
			// + userP.getUserid()
			// + "' and childid='"
			// + user.getId() + "' and fnumber<>0");
			// if (list1 != null && list1.size() > 0 && list1.get(0) != null) {
			// user.setPaterRatio((list1.get(0)).toString());
			// System.out.println(list1.get(0));
			// } else {
			// user.setPaterRatio("");
			// }
			// }
			modelMap.put("result", list);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			System.out.print("hehh" + e.getMessage());
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 13.兑换记录
	@RequestMapping("/getExchangeHistory")
	public ModelAndView getExchangeHistory(HttpServletRequest request,
			HttpServletResponse response, @RequestParam("userId") String userId) {
		String page1 = request.getParameter("page");
		int page = 1;
		if (page1 != null)
			page = Integer.parseInt(page1);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			User user = userManager.getById(userId);
			CreditsExchangeQuery query = new CreditsExchangeQuery();
			query.setUserid(user.getUserid());
			query.setPageSize(20);
			query.setPageNumber(page);
			query.setSortColumns(" crdate  desc");
			List<CreditsExchange> list = creditsExchangeManager.findPage(query)
					.getResult();
			modelMap.put("result", list);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 14. 达人榜
	@RequestMapping("/getTop50")
	public ModelAndView getTop50(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			UserQuery query = new UserQuery();
			query.setSortColumns(" integral  desc");
			query.setPageSize(20);
			List<User> list = userManager.searchPage(query).getResult();
			List<Map<String, String>> mlist = new ArrayList<Map<String, String>>();
			for (User user : list) {
				Map<String, String> map = new HashMap<String, String>();
				map.put("nickName", user.getUsername());
				map.put("totalNum", user.getIntegral() + "");
				mlist.add(map);
			}
			modelMap.put("result", mlist);
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 14. 编辑信息
	@RequestMapping("/editInfo")
	public ModelAndView editInfo(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("nickName") String nickName,
			@RequestParam("email") String email,
			@RequestParam("phone") String phone) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			// nickName=new String(nickName.getBytes("ISO-8859-1"),"utf-8");
			User user = userManager.getById(userId);
			user.setUsername(nickName);
			user.setEamil(email);
			user.setPhone(phone);
			userManager.update(user);
			ResultStatusDto result = new ResultStatusDto("100", "保存成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 16. 确认邀请
	@RequestMapping("/enterInvite")
	public ModelAndView enterInvite(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("inviteNum") String inviteNum) {
		User user = userManager.getById(userId);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		if (user != null) {
			try {
				List<User> list = userManager.getHql("from User where userid='"
						+ inviteNum + "'");
				if (list != null && list.size() > 0) {// user22邀请者，user被邀请者
					User user22 = list.get(0);
					if (user.getRegister().compareTo(user22.getRegister()) > 0) {
						// Setting setting1 = settingManager.getType("vipnum");
						int vipnum = settingManager.vipnum;
						user.setPaterid(user22.getId());
						user.setPaterDate(DateTimeUtil.getDateTimeString());
						user22.setExtensionNumber(user22.getExtensionNumber() + 1);
						if (user22.getIsvip() == 1 && vipnum > 0) {
							ObtainIntegral obint = new ObtainIntegral();
							obint.setUserid(user.getUserid());
							obint.setUsername(user.getUsername());
							obint.setNumber(vipnum);
							obint.setFnumber(vipnum);
							obint.setType(6);
							obint.setRemark("");
							obint.setCrdate(DateTimeUtil.getDateTimeString());
							obtainIntegralManager.save(obint);
							user.setIntegral(user.getIntegral() + vipnum);
							user.setDintegral(user.getDintegral() + vipnum);
						}
						ObtainIntegral obint = new ObtainIntegral();
						obint.setUserid(user.getUserid());
						obint.setUsername(user.getUsername());
						obint.setNumber(20);
						obint.setFnumber(20);
						obint.setType(3);
						obint.setRemark("");
						obint.setCrdate(DateTimeUtil.getDateTimeString());
						obtainIntegralManager.save(obint);
						user.setIntegral(user.getIntegral() + 20);
						user.setDintegral(user.getDintegral() + 20);
						user.setPaterRatio(settingManager.ratio + "%");
						userManager.update(user);
						userManager.update(user22);
						ResultStatusDto result = new ResultStatusDto("100",
								"邀请成功");
						modelMap.put("status", result);
					} else {
						ResultStatusDto result = new ResultStatusDto("105",
								"不能推荐比你早创建的用户");
						modelMap.put("status", result);
					}
				} else {
					ResultStatusDto result = new ResultStatusDto("103", "没有此用户");
					modelMap.put("status", result);
				}
			} catch (Exception e) {
				ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
				modelMap.put("status", result);
			}
		} else {
			ResultStatusDto result = new ResultStatusDto("144", "没有此用户");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 16. 推广链接
	@RequestMapping("/toInvite")
	public ModelAndView toInvite(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("inviteNum") String inviteNum) {
		User user = userManager.getById(userId);
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		if (user != null) {
			try {
				List<User> list = userManager.getHql("from User where userid='"
						+ inviteNum + "'");
				if (list != null && list.size() > 0) {// user22邀请者，user被邀请者
					User user22 = list.get(0);
					if (user.getRegister().compareTo(user22.getRegister()) > 0) {
						user.setPaterid(user22.getId());
						user.setPaterDate(DateTimeUtil.getDateTimeString());
						user22.setExtensionNumber(user22.getExtensionNumber() + 1);
						user.setPaterRatio(settingManager.ratio + "%");
						userManager.update(user);
						userManager.update(user22);
						ResultStatusDto result = new ResultStatusDto("100",
								"邀请成功");
						modelMap.put("status", result);
					} else {
						ResultStatusDto result = new ResultStatusDto("105",
								"不能接受比你后创建的用户推荐");
						modelMap.put("status", result);
					}
				} else {
					ResultStatusDto result = new ResultStatusDto("103", "没有此用户");
					modelMap.put("status", result);
				}
			} catch (Exception e) {
				ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
				modelMap.put("status", result);
			}
		} else {
			ResultStatusDto result = new ResultStatusDto("144", "没有此用户");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 16. 自动成为下线链接
	@RequestMapping("/invite")
	public String invite(HttpServletRequest request,
			HttpServletResponse response) {// invite.json?XXXXX
		String str = request.getQueryString();
		if (str != null && !str.equals("")) {
			List<User> list = userManager.getHql("from User where userid='"
					+ str + "'");
			if (list != null && list.size() > 0 && list.get(0) != null) {
				AutoPater autoPater = new AutoPater();
				autoPater.setPaterId(list.get(0).getId());
				autoPater.setIp(request.getRemoteAddr());
				autoPater.setCrdate(DateTimeUtil.getDateTimeString());
				autoPaterManager.save(autoPater);
			}
		}
		//
		try {
			response.sendRedirect("http://www.jizhuan.cc");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 17. 消息盒子
	@RequestMapping("/getMessage")
	public ModelAndView getMessage(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {

			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "查询出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 18. 意见反馈
	@RequestMapping("/submitAdvice")
	public ModelAndView submitAdvice(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("comment") String comment) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			/*
			 * try { comment=new String(comment.getBytes("ISO-8859-1"),"utf-8");
			 * } catch (UnsupportedEncodingException e) { }
			 */
			User user = userManager.getById(userId);
			Suggestion suggestion = new Suggestion();
			suggestion.setUserid(user.getUserid());
			suggestion.setUsername(user.getUsername());
			suggestion.setCrdate(DateTimeUtil.getDateTimeString());
			suggestion.setContent(comment);

			suggestionManager.save(suggestion);
			ResultStatusDto result = new ResultStatusDto("100",
					"我们已经收到您的反馈，谢谢。");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "后台出错了");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 19. 使用帮助()
	@RequestMapping("/usinghelp")
	public ModelAndView usinghelp(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();

		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 19. 消息盒子
	@RequestMapping("/getMessageList")
	public ModelAndView getMessageList(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();

		List<Messages> list = messagesManager
				.getHql("from Messages order by crdate desc");
		modelMap.put("result", list);
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 19. 版本信息
	@RequestMapping("/getVersions")
	public ModelAndView getVersions(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		try {
			Setting setting = settingManager.getType("versions");
			if (setting == null) {
				modelMap.put("versions", "1");
			} else {
				modelMap.put("versions", setting.getText());
			}
			modelMap.put("url", "http://121.199.35.52:8080/Pay/Zhuangke.apk");
			ResultStatusDto result = new ResultStatusDto("100", "获取成功");
			modelMap.put("status", result);
		} catch (Exception e) {
			ResultStatusDto result = new ResultStatusDto("104", "服务器异常");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 是否打开假页面
	@RequestMapping("/openPage")
	public ModelAndView openPage(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		Setting setting3 = settingManager.getType("YesNo");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		modelMap.put("isf1", "no");
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 后台配置分钟时间页面
	@RequestMapping("/integralInterval")
	public ModelAndView interval(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.integral;
		modelMap.put("interval", settingManager.interval);
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 后台配置兑换时间页面
	@RequestMapping("/exchageInterval")
	public ModelAndView exchage(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("exchange");
		modelMap.put("interval", Double.toString(settingManager.exchange));
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 上下线比率获取页面
	@RequestMapping("/ratio")
	public ModelAndView radio(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("ratio");
		modelMap.put("ratio", Integer.toString(settingManager.ratio));
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 更新内容获取页面
	@RequestMapping("/updateInfo")
	public ModelAndView updateInfo(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("updateInfo");
		modelMap.put("updateInfo",
				settingManager.updateInfo.replace("\\n", "\n"));
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 19. 版本信息
	@RequestMapping("/submitJoke")
	public ModelAndView sfsfds(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("nsstring") String nsstring) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		Setting setting3 = settingManager.getType("YesNo");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		modelMap.put("isf", "no");
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 是否打开假页面
	@RequestMapping("/openPage2")
	public ModelAndView openPage2(HttpServletRequest request,
			HttpServletResponse response) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 积分回调
	@RequestMapping("/adpoint")
	public String adpoint(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		try {
			File file = new File("c:\\Result.txt");
			if (!file.exists())
				file.createNewFile();
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
					"c:\\Result.txt"), true));

			while (m.hasMoreElements()) {
				String str = (String) m.nextElement();
				writer.write(str + "=" + request.getParameter(str));

			}
			writer.write("\r\n\r\n");
			writer.close();

		} catch (Exception e) {

		}
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 19. 版本信息
	@RequestMapping("/submitJoke2")
	public ModelAndView submitJoke2(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("nsstring") String nsstring) {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		Setting setting3 = settingManager.getType("YesNo");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		modelMap.put("isf", setting3.getText());
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 积分回调
	@RequestMapping("/adpointofyoumi")
	public String adpointofyoumi(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\有米.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\有米.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("user");
		int point = Integer.parseInt(request.getParameter("points"));
		String appName = request.getParameter("ad");
		String remark = "有米";
		setObint(IDFA, point, appName, remark);
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofmidi")
	public String adpointofmidi(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\米迪.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\米迪.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("imei");
		int point = Integer.parseInt(request.getParameter("cash"));
		String appName = request.getParameter("appName");
		String remark = "米迪";
		setObint(IDFA, point, appName, remark);

		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofguomeng")
	public String adpointofguomeng(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\果盟.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\果盟.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("device");
		int point = Integer.parseInt(request.getParameter("points"));
		String appName = request.getParameter("ad");
		String remark = "果盟";
		setObint(IDFA, point, appName, remark);
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofdianru")
	public Map adpointofdianru(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// try {
		// File file = new File("c:\\点入.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\点入.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		try {
			String IDFA = request.getParameter("userid");
			int point = Integer.parseInt(request.getParameter("point"));
			String appName = request.getParameter("adname");
			String remark = "点入";
			setObint(IDFA, point, appName, remark);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			modelMap.put("success", "false");
			modelMap.put("message", e.getMessage());
			return modelMap;
		}
		modelMap.put("success", "true");
		modelMap.put("message", "");
		return modelMap;
	}

	// 积分回调
	@RequestMapping("/adpointofdianle")
	public String adpointofdianle(HttpServletRequest request,
			HttpServletResponse response) {
		// 如果通知成功，请输出一个字符串200，不是http的状态码。否则会重复通知
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\点乐.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\点乐.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("snuid");
		int point = Integer.parseInt(request.getParameter("currency"));
		String appName = request.getParameter("ad_name");
		String remark = "点乐";
		setObint(IDFA, point, appName, remark);

		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofanwo")
	public String adpointofanwo(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\安沃.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\安沃.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("idfa");
		int point = Integer.parseInt(request.getParameter("point"));
		String appName = request.getParameter("adname");
		String remark = "安沃";
		setObint(IDFA, point, appName, remark);
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofyijifen")
	public Map adpointofyijifen(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// try {
		// File file = new File("c:\\易积分.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\易积分.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		try {
			String IDFA = request.getParameter("idfa");
			if (IDFA == null)
				IDFA = request.getParameter("uuid");
			int point = Integer.parseInt(request.getParameter("score"));
			String appName = request.getParameter("adId");
			String remark = "易积分";
			setObint(IDFA, point, appName, remark);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			modelMap.put("success", "false");
			modelMap.put("message", e.getMessage());
			return modelMap;
		}
		modelMap.put("success", "true");
		modelMap.put("message", "");
		return modelMap;
	}

	// 积分回调
	@RequestMapping("/adpointoflimei")
	public String adpointoflimei(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\力美.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\力美.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("idfa");
		int point = Integer.parseInt(request.getParameter("point"));
		if (IDFA == null)
			IDFA = request.getParameter("uid");
		String appName = request.getParameter("title");
		String remark = "力美";
		setObint(IDFA, point, appName, remark);

		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofduomeng")
	public String adpointofduomeng(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\多盟.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\多盟.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("user");
		int point = Integer.parseInt(request.getParameter("point"));
		String appName = request.getParameter("ad");
		String remark = "多盟";
		setObint(IDFA, point, appName, remark);

		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofwanpu")
	public String adpointofwanpu(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\万普.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\万普.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("udid");
		int point = Integer.parseInt(request.getParameter("points"));
		String appName = request.getParameter("ad_name");
		String remark = "万普";
		setObint(IDFA, point, appName, remark);
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofmopan")
	public String adpointofmopan(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\磨盘.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\磨盘.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }

		String IDFA = request.getParameter("imei");
		int point = Integer.parseInt(request.getParameter("cash"));
		String appName = request.getParameter("appShowName");
		String remark = "磨盘";
		setObint(IDFA, point, appName, remark);

		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofaidesiqi")
	public String adpointofaidesiqi(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\艾德思奇.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\艾德思奇.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("deviceid");
		int point = Integer.parseInt(request.getParameter("points"));
		String appName = request.getParameter("adname");
		String remark = "艾德思奇";
		setObint(IDFA, point, appName, remark);
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofaipudongli")
	public String adpointofaipudongli(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\爱普动力.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\爱普动力.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("idfa");
		Float points = Float.parseFloat(request.getParameter("point"));
		if (points != null) {
			int point = (int) (float) points;
			String appName = request.getParameter("appname");
			String remark = "爱普动力";
			setObint(IDFA, point, appName, remark);
		}
		try {
			response.getWriter().print("1");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointochukong")
	public String adpointochukong(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\触控.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\触控.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("idfa");
		Float points = Float.parseFloat(request.getParameter("coins"));
		if (points != null) {
			if (IDFA == null)
				IDFA = request.getParameter("mac");
			int point = (int) (float) points;
			String appName = request.getParameter("adtitle");
			String remark = "触控";
			setObint(IDFA, point, appName, remark);
		}
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointozhimeng")
	public String adpointozhimeng(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\指盟.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\指盟.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("idfa");
		Float points = Float.parseFloat(request.getParameter("points"));
		if (points != null) {
			if (IDFA == null)
				IDFA = request.getParameter("mac");
			int point = (int) (float) points;
			String appName = request.getParameter("appname");
			String remark = "指盟";
			setObint(IDFA, point, appName, remark);
		}
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointochukongjizhuan")
	public String adpointochukongjizhuan(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();

		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", result);
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addObject("200");
		// try {
		// File file = new File("c:\\触控机赚.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\触控机赚.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		String IDFA = request.getParameter("idfa");
		Float points = Float.parseFloat(request.getParameter("coins"));
		if (points != null) {
			if (IDFA == null)
				IDFA = request.getParameter("mac");
			int point = (int) (float) points;
			String appName = request.getParameter("adtitle");
			String remark = "触控机赚";
			setObint(IDFA, point, appName, remark);
		}
		try {
			response.getWriter().print("200");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/adpointofxingyun")
	public Map adpointofxingyun(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		try {
			File file = new File("c:\\行云.txt");
			if (!file.exists())
				file.createNewFile();
			BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
					"c:\\行云.txt"), true));

			while (m.hasMoreElements()) {
				String str = (String) m.nextElement();
				writer.write(str + "=" + request.getParameter(str));

			}
			writer.write("\r\n\r\n");
			writer.close();

		} catch (Exception e) {

		}
		try {
			String IDFA = request.getParameter("idfa");
			int point = Integer.parseInt(request.getParameter("score"));
			if (IDFA == null)
				IDFA = request.getParameter("uuid");
			String appName = URLDecoder.decode(request.getParameter("appName"),
					"utf-8");
			String remark = "行云";
			setObint(IDFA, point, appName, remark);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			modelMap.put("success", "false");
			modelMap.put("message", e.getMessage());
			return modelMap;
		}
		modelMap.put("success", "true");
		modelMap.put("message", "");
		return modelMap;
	}

	// 积分回调
	@RequestMapping("/adpointofjupeng")
	public String adpointofjupeng(HttpServletRequest request,
			HttpServletResponse response) {
		Enumeration m = request.getParameterNames();
		Map<String, Object> modelMap = new HashMap<String, Object>();
		// try {
		// File file = new File("c:\\巨朋.txt");
		// if (!file.exists())
		// file.createNewFile();
		// BufferedWriter writer = new BufferedWriter(new FileWriter(new File(
		// "c:\\巨朋.txt"), true));
		//
		// while (m.hasMoreElements()) {
		// String str = (String) m.nextElement();
		// writer.write(str + "=" + request.getParameter(str));
		//
		// }
		// writer.write("\r\n\r\n");
		// writer.close();
		//
		// } catch (Exception e) {
		//
		// }
		try {
			String IDFA = request.getParameter("userdef");
			int point = Integer.parseInt(request.getParameter("score"));
			String appName = request.getParameter("adid");
			String remark = "巨朋";
			setObint(IDFA, point, appName, remark);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			try {
				response.getWriter().print("fail");
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		try {
			response.getWriter().print("success");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/a")
	public ModelAndView a(HttpServletRequest request,
			HttpServletResponse response) throws IOException {

		// int point =Integer.parseInt(request.getParameter("points"));
		// User user = userManager.getById(userId);
		byte bytes[];
		String str = null;
		str = request.getQueryString();

		bytes = str.getBytes();
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte) (bytes[i] ^ secret);
		}
		str = new String(bytes);
		System.out.println("字符串" + str);
		// str=request.getQueryString();
		// String str1=new String(bytes);
		// Enumeration m = request.getParameterNames();
		// Gson gson = new Gson();
		// Map<String, String> map = new HashMap<String, String>();
		// map = gson.fromJson(str, Map.class);
		// String remark = (String) map.get("remark");
		//
		// System.out.println("线程名"+Thread.currentThread().getName().toString());
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		// Setting setting3 = settingManager.getType("isf");
		ResultStatusDto result = new ResultStatusDto("100", "获取成功");
		modelMap.put("status", "呵呵");
		// modelMap.put(setting3.getRemark(), setting3.getText());
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	// 积分回调
	@RequestMapping("/abc")
	public String abc(HttpServletRequest request, HttpServletResponse response)
			throws IOException {

		try {
			response.setCharacterEncoding("gb2312");
			// response.getWriter().print(Sudatong.recharge1(2,"227751753"));
			response.getWriter().print(Sudatong.status("11111111"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 积分回调
	@RequestMapping("/addIDFA")
	public ModelAndView addIDFA(HttpServletRequest request,
			HttpServletResponse response,
			@RequestParam("userId") String userId,
			@RequestParam("IDFA") String IDFA, @RequestParam("MAC") String MAC,
			@RequestParam("app") int app) throws IOException {
		Map<String, Object> modelMap = new HashMap<String, Object>();
		ModelAndView modelView = new ModelAndView();
		User user = userManager.getById(userId);
		if (user != null) {
			user.setIDFA(IDFA);
			user.setMAC(MAC);
			user.setApp(app);
			userManager.update(user);
			ResultStatusDto result = new ResultStatusDto("100", "增加成功");
			modelMap.put("status", result);
		} else {
			ResultStatusDto result = new ResultStatusDto("103", "没有此用户");
			modelMap.put("status", result);
		}
		modelView.addAllObjects(modelMap);
		return modelView;
	}

	private void setObint(String IDFA, int point, String appName, String remark) {
		// if (keyId != null) {
		// if (keyIds.contains(keyId))
		// return;// 去除重复keyId
		// List list1 = obtainIntegralManager
		// .getHql("from ObtainIntegral where appName='"
		// + appName
		// + "' and remark='"
		// + remark + "' and userId='"
		// + remark + "' and fnumber<>0");// ip限制
		// if (list1 != null && list1.size() > 0 && list1.get(0) != null) {
		// return;
		// }else
		// keyIds.add(keyId);
		// }

		if (IDFA != null && appName != null && remark != null) {
			Date nowDate = new Date();
			if (appName.matches("\\d+")) {
				List<Ad> list = adManager.getHql("from Ad where adId='"
						+ appName + "' and remark='" + remark + "'");
				if (list != null && list.size() > 0 && list.get(0) != null) {
					Ad ad = list.get(0);
					appName = ad.getAppName();
				}
			}
			IDFA = IDFA.toLowerCase();
			if (IDFA.contains(":"))
				IDFA = IDFA.replace(":", "");
			List<User> list = userManager.getHql("from User where IDFA='"
					+ IDFA + "' or MAC='" + IDFA + "'");
			if (list != null && list.size() > 0 && list.get(0) != null) {
				User user = list.get(0);
				List list3 = obtainIntegralManager
						.getHql("from ObtainIntegral where  userid='"
								+ user.getUserid() + "' and remark='" + remark
								+ "' and appName='" + appName + "'");
				if (list3 != null && list3.size() > 0 && list3.get(0) != null) {
					point = 0;
				}
				int isErr = 0;
				if (point != 0) {
					// 限制同一IP的最大用户数（后台可配置）start
					SimpleDateFormat dateFm = new SimpleDateFormat(
							"yyyy-MM-dd 00:00:00");
					String time = dateFm.format(new java.util.Date());
					String previousDateTime = DateTimeUtil
							.getPreviousDateTimeString(3);// 72小时内
					//
					// List<ObtainIntegral>
					// list=obtainIntegralManager.getHql("from ObtainIntegral where IP='"+request.getRemoteAddr()+"' and crdate>='"+time+"'"
					// );
					// Setting setting2 = settingManager.getType("usernum");
					int usernum = settingManager.usernum;
					// Setting setting3 = settingManager.getType("usernum1");
					int usernum1 = settingManager.usernum1;
					// Setting setting4 = settingManager.getType("usernum2");
					int usernum2 = settingManager.usernum2;
					// Setting setting5 = settingManager.getType("num");
					int num1 = settingManager.num;
					int interval = SettingManager.pushInterval;

					// 黑名单ip end
					List<ObtainIntegral> list2 = obtainIntegralManager
							.getHql(" from ObtainIntegral where userid='"
									+ user.getUserid()
									+ "' order by crdate desc");
					if (list2 != null && list2.size() > 0
							&& list2.get(0) != null) {
						String firstId = null;
						ObtainIntegral obtainIntegral = list2.get(0);
						String IP = obtainIntegral.getIP();
						String id = obtainIntegral.getId();
						if (IP != null) {
							if (list2.size() > 1) {
								for (int i = list2.size() - 1; i > 0; i--) {
									if (list2.get(i).getIP() != null
											&& list2.get(i).getIP().equals(IP)) {
										firstId = list2.get(i).getId();
										break;
									}
								}
							}
						}
						Date crDate = DateTimeUtil.toDate(obtainIntegral
								.getCrdate());

						double differ = DateTimeUtil.compareSecond(nowDate,
								crDate);
						if (differ > interval) {

						} else {
							try {
								File file = new File("c:\\time.txt");
								if (!file.exists())
									file.createNewFile();
								BufferedWriter writer = new BufferedWriter(
										new FileWriter(
												new File("c:\\time.txt"), true));
								SimpleDateFormat dateFm1 = new SimpleDateFormat(
										"yyyy-MM-dd HH:mm:ss");
								String str = dateFm1.format(nowDate);
								writer.write(str + " " + differ + " "
										+ interval + " "
										+ obtainIntegral.getCrdate());

								writer.write("\r\n\r\n");
								writer.close();

							} catch (Exception e) {

							}
							isErr = 2;// 表示推送间隔太短异常
						}
						if (isErr == 0 && IP != null) {

							if (usernum > 0 && isErr == 0) {
								List list1 = null;
								if (firstId == null)
									list1 = obtainIntegralManager
											.getHql("select DISTINCT userid from ObtainIntegral where id<>'"
													+ id
													+ "' and IP='"
													+ IP
													+ "' and crdate>='"
													+ previousDateTime
													+ "' and fnumber<>0");// ip限制
								else
									list1 = obtainIntegralManager
											.getHql("select DISTINCT userid from ObtainIntegral where id<>'"
													+ id
													+ "' and id<>'"
													+ firstId
													+ "' and IP='"
													+ IP
													+ "' and crdate>='"
													+ previousDateTime
													+ "' and fnumber<>0");// ip限制
								if (list1 != null && list1.size() >= usernum
										&& list1.get(0) != null) {
									if (!list1.contains(user.getUserid()))
										isErr = 3;
								}
							}
							String str = IP.substring(0, IP.lastIndexOf('.'));
							String str1 = str
									.substring(0, str.lastIndexOf('.'));
							if (usernum1 > 0 && isErr == 0) {
								List list1 = null;
								if (firstId == null)
									list1 = obtainIntegralManager
											.getHql("select DISTINCT userid from ObtainIntegral where id<>'"
													+ id
													+ "' and IP like '"
													+ str
													+ "%' and crdate>='"
													+ time + "' and fnumber<>0");// ip限制
								else
									list1 = obtainIntegralManager
											.getHql("select DISTINCT userid from ObtainIntegral where id<>'"
													+ id
													+ "' and id<>'"
													+ firstId
													+ "' and IP like '"
													+ str
													+ "%' and crdate>='"
													+ time
													+ "' and fnumber<>0");// ip限制
								if (list1 != null && list1.size() >= usernum1
										&& list1.get(0) != null) {
									if (!list1.contains(user.getUserid()))
										isErr = 1;
								}
							}

							if (usernum2 > 0 && isErr == 0) {
								List list1 = null;
								if (firstId == null)
									list1 = obtainIntegralManager
											.getHql("select DISTINCT userid from ObtainIntegral where id<>'"
													+ id
													+ "' and IP like '"
													+ str1
													+ "%' and crdate>='"
													+ time + "' and fnumber<>0");// ip限制
								else
									list1 = obtainIntegralManager
											.getHql("select DISTINCT userid from ObtainIntegral where id<>'"
													+ id
													+ "' and id<>'"
													+ firstId
													+ "' and IP like '"
													+ str1
													+ "%' and crdate>='"
													+ time
													+ "' and fnumber<>0");// ip限制
								if (list1 != null && list1.size() >= usernum2
										&& list1.get(0) != null) {
									if (!list1.contains(user.getUserid()))
										isErr = 1;
								}
							}

						}
					}

				}
				ObtainIntegral obint = new ObtainIntegral();
				obint.setUserid(user.getUserid());
				obint.setUsername(user.getUsername());
				obint.setNumber(point);
				SimpleDateFormat dateFm = new SimpleDateFormat(
						"yyyy-MM-dd HH:mm:ss");
				String str = dateFm.format(nowDate);
				if (isErr == 0) {
					obint.setFnumber(point);
					if (point != 0 && user.getPaterid() != null
							&& !user.getPaterid().equals("")) {
						User userP = userManager.getById(user.getPaterid());
						// Setting setting = settingManager.getType("ratio");
						int ratio = settingManager.ratio;
						int integral = SettingManager.integral;
						int sss = (point * ratio) / 100;
						int num = (integral * ratio) / 100 - 1;
						if (sss > 0 && sss <= num) {// 不超过 才返回
							// myNum=addNum-sss;
							ObtainIntegral obint1 = new ObtainIntegral();
							obint1.setUserid(userP.getUserid());
							obint1.setUsername(userP.getUsername());
							obint1.setNumber(sss);
							obint1.setFnumber(sss);
							obint1.setType(1);
							obint1.setRemark(remark);
							obint1.setAppName(appName);
							obint1.setChildid(user.getId());// 奖励添加下线id
							obint1.setCrdate(str);
							obtainIntegralManager.save(obint1);
							userP.setIntegral(userP.getIntegral() + sss);
							userP.setDintegral(userP.getDintegral() + sss);
							userManager.update(userP);
						}
					}
				}
				obint.setType(1);
				obint.setRemark(remark);
				obint.setAppName(appName);
				obint.setCrdate(str);
				// if (keyId != null) {
				// Order order = new Order();
				// order.setCrdate(str);
				// order.setOrderId(keyId);
				// orderManager.save(order);
				// if (keyIds.contains(keyId))
				// keyIds.remove(keyId);
				// }
				obtainIntegralManager.save(obint);
				if (point != 0) {
					if (isErr == 0) {
						userManager.update("UPDATE User SET integral=integral+"
								+ point + ", dintegral=dintegral+" + point
								+ " WHERE id='" + user.getId() + "'");
					} else {
						userManager
								.update("UPDATE User SET dintegral=dintegral+"
										+ point + " WHERE id='" + user.getId()
										+ "'");
					}
					if (isErr != 1)
						JPushClientExample.send(user.getId(), appName, point,
								user.getApp(), isErr);
				}
			}
		}
	}

	private String deCode(String str) {
		String json = null;
		byte bytes[] = str.getBytes();
		for (int i = 0; i < bytes.length; i++) {
			bytes[i] = (byte) (bytes[i] ^ secret);
		}
		json = new String(bytes);
		return json;
	}

	class statusTask extends TimerTask {
		private int k = 0;
		private String orderId;
		private CreditsExchange crachange;
		private Timer timer;

		public statusTask(String orderId, CreditsExchange crachange, Timer timer) {
			this.orderId = orderId;
			this.crachange = crachange;
			this.timer = timer;
		}

		@Override
		public void run() {
			// TODO Auto-generated method stub
			int status=Sudatong.status(orderId) ;
			if (status == 1 || status == -1
					|| status == 9) {
				crachange.setState("2");
				crachange.setRemark("自动充值成功");
				creditsExchangeManager.update(crachange);
				timer.cancel();
			}else if (status == -9 || status == 10) {
				crachange.setRemark("自动充值失败");
				creditsExchangeManager.update(crachange);
				timer.cancel();
			}
			if (k == 3) {
				crachange.setRemark("自动充值失败");
				creditsExchangeManager.update(crachange);
				timer.cancel();
			}
			++k;
		}

	}

}
